class ClassificationController < ApplicationController

  before_filter :authenticate
  
  def index
    @classifications = Classification.find(:all, :conditions => "classification_id is null" ,:order => "name ASC")
  end
  
  #=begin
  # Lista os filhos relacionados com a classificação
  #=end
  def listchild
    @classifications = Classification.find(:all, :conditions => ["classification_id = (?)", params[:id] ] ,:order => "name ASC")
    render :update do |page|
      if @classifications.length > 0
        page.replace_html "classification_#{params[:id]}" , :partial => 'list'
      else
        page.replace_html "classification_#{params[:id]}" , "<ul><li>Não existe classificações associadas.</li></ul>"
      end
      if params[:displayform] == 'none'
        page.hide "form_classification_#{params[:id]}"
      end
    end
  end
  
  def form
    if params[:act] == "alter"
      @classification = Classification.find(params[:id])
    elsif params[:act] == "create"
      @classification = Classification.new
      @classification[:classification_id] = params[:id]
    end
    render :update do |page|
      page.replace_html "form_classification_#{params[:id]}" , :partial => 'form'
      page.show "form_classification_#{params[:id]}"
    end
  end
  
  def alter
    @classification = Classification.find(params[:classification][:id])
    if request.post?
      @classification.attributes = params[:classification]
      if @classification.save
        if @classification[:classification_id]
          redirect_to :action => "listchild", :id => @classification[:classification_id], :displayform => "none"
        else
          redirect_to :action => "listroot"
        end
      end
    end
  end

  def create
    @classification = Classification.new
    if request.post?
      @classification.attributes = params[:classification]
      if @classification.save
        redirect_to :action => "listchild", :id => @classification[:classification_id], :displayform => "none"
      end
    end
  end
  
  def listroot
    @classifications = Classification.find(:all, :conditions => "classification_id is null" ,:order => "name ASC")
    render :update do |page|
      page.replace_html "classification" , :partial => 'list'
    end
  end
  
  def remove
    @classification = Classification.find(params[:id])
    @classifications_childs = Classification.find(:all, :conditions => ["classification_id = (?)",@classification[:id]])
    if @classifications_childs.length > 0
      render :update do |page|
        page << "alert('Desculpe, essa classificação não pode ser removida por conter outras classificações associadas.');"
      end
    else
    if @classification.destroy
        @classifications = Classification.find(:all, :conditions => ["classification_id = (?)", params[:classification_id] ] ,:order => "name ASC")
        render :update do |page|
          if @classifications.length > 0
            page.replace_html "classification_#{params[:classification_id]}" , :partial => 'list'
          else
            page.replace_html "classification_#{params[:classification_id]}" , ""
          end
        end
    end
    end
  end

  protected
  
=begin
Filtro de autenticação do controler
=end
  def authenticate
    authenticate_check('(ADM:ALL||CLAS:VIEW||CLAS:ALL)','/')
  end
  
end
